package com.dangz.dans.controller;

import com.dangz.dans.entity.CommonReqEntity;
import com.dangz.dans.entity.CommonResponse;
import com.dangz.dans.model.wx.SubscribeInfo;
import com.dangz.dans.model.req.SubscribeInfoPageReq;
import com.dangz.dans.service.SubscribeInfoServiceImpl;
import com.dangz.dans.response.ApiEnum;
import com.dangz.dans.response.CodeEnum;
import com.dangz.dans.response.TypeEnum;
import com.dangz.dans.utils.PageUtil;
import com.dangz.dans.utils.RedisUtil;
import com.dangz.dans.utils.WxUtils;
import io.swagger.annotations.*;
import org.apache.http.HttpHeaders;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 粉丝基本接口
 * @author dangz
 * @since 2018/2/1 16:30
 */
@RestController
@RequestMapping("dans/subscribeInfo")
@Api(tags = "wx_粉丝操作")
public class SubscribeInfoController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private SubscribeInfoServiceImpl subscribeInfoService;
    @Autowired
    private RedisUtil redisUtil;

    @ApiOperation(value = "拉取粉丝", notes = "POST方式")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", value = "Authorization", name = HttpHeaders.AUTHORIZATION, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "appid", value = "APPID", dataType = "String", required = true),
            @ApiImplicitParam(paramType = "query", name = "secret", value = "SECRET", dataType = "String", required = true)
    })
    @PostMapping("getSubscribeInfos")
    public CommonResponse findByCode(String appid, String secret) {
        CommonResponse commonResponse = null;
        try {
            List<SubscribeInfo> obj = WxUtils.getOpenIdInfo(appid, secret, redisUtil, "");
            subscribeInfoService.save(obj);
            if (obj != null) {
                commonResponse = new CommonResponse(TypeEnum.getFullRespCode(TypeEnum.Success,
                        ApiEnum.WXMK, CodeEnum.SUCCESS_SERVICE_LOCAL), CodeEnum.SUCCESS_SERVICE_LOCAL.getInfo());
            } else {
                commonResponse = new CommonResponse(TypeEnum.getFullRespCode(TypeEnum.Success,
                        ApiEnum.WXMK, CodeEnum.SUCCESS_NO_DATA_LOCAL), CodeEnum.SUCCESS_NO_DATA_LOCAL.getInfo());
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("粉丝拉取异常……");
            commonResponse = new CommonResponse(TypeEnum.getFullRespCode(TypeEnum.Error,
                    ApiEnum.WXMK, CodeEnum.FAIL_SERVICE_LOCAL), CodeEnum.FAIL_SERVICE_LOCAL.getInfo());
        }
        return commonResponse;
    }


    @ApiOperation(value = "分页", notes = "POST方式")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", value = "Authorization", name = HttpHeaders.AUTHORIZATION, dataType = "String")
    })
    @GetMapping("listPage")
    public CommonResponse listPage(@ApiParam(required = true) @RequestBody CommonReqEntity<SubscribeInfoPageReq> req){
        CommonResponse commonResponse = null;
        PageUtil page = null;
        try {
            page = subscribeInfoService.listPage(req);
            if (page == null) {
                commonResponse = new CommonResponse(TypeEnum.getFullRespCode(TypeEnum.Success,
                        ApiEnum.WXMK, CodeEnum.SUCCESS_SERVICE_LOCAL), CodeEnum.SUCCESS_SERVICE_LOCAL.getInfo());
            } else {
                commonResponse = new CommonResponse(TypeEnum.getFullRespCode(TypeEnum.Success,
                        ApiEnum.WXMK, CodeEnum.SUCCESS_SERVICE_LOCAL), CodeEnum.SUCCESS_SERVICE_LOCAL.getInfo(), page);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("粉丝分页异常……");
            commonResponse = new CommonResponse(TypeEnum.getFullRespCode(TypeEnum.Error,
                    ApiEnum.WXMK, CodeEnum.FAIL_SERVICE_LOCAL), CodeEnum.FAIL_SERVICE_LOCAL.getInfo());
        }
        return commonResponse;
    }
}
